package com.okilo.online.app.fmt

import android.R
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import com.okilo.online.app.common.OkiloComFmt
import com.okilo.online.app.event.OkiloEventData
import com.okilo.online.app.tools.helper.OkiloConfigProcessHp

/**
 * OkiloLoan
 * @Author wwdeng
 * @CreateTime: 2024/12/10 14:53
 * @Annotation：首页
 */
class OkiloTabHomeFmt : OkiloComFmt() {

    //首页额度信息
    private var homeLimitFmt: OkiloHomeLimitFmt? = null

    //首页申贷状态
    private var homeApplyStatusFmt: OkiloHomeApplyStatusFmt? = null

    override val pageName: String = "Home_Page"

    override val enablePageTrack: Boolean = false

    override fun createOkiloContentView(inflater: LayoutInflater): View? {
        return null
    }

    override fun initializeOkiloViews() {
    }

    override fun setupOkiloEventListeners() {
    }

    override fun loadOkiloData() {
//        checkOkiloHomeShowPage()
    }

    /**
     * 切换显示
     */
    private fun checkOkiloHomeShowPage(){
        //审核中 || 放款中 || 已拒绝
        val isReviewing = OkiloConfigProcessHp.iOkiloApplyReviewing()
        val isDisbursing = OkiloConfigProcessHp.iOkiloApplyDisbursing()
        val isRejected = OkiloConfigProcessHp.iOkiloApplyRejected()
        if (isReviewing or isDisbursing or isRejected){
            sOkiloHomeApplyStatusPage()
            return
        }
        //待还款 || 首页
        sOkiloHomeLimitPage()
    }

    /**
     * 显示首页额度信息
     */
    @SuppressLint("CommitTransaction")
    private fun sOkiloHomeLimitPage(){
        homeApplyStatusFmt = null
        if (homeLimitFmt == null){
            homeLimitFmt = OkiloHomeLimitFmt()
            homeLimitFmt?.let {
                childFragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
                    .replace(rootViewBinding.rootContainer.id,it)
                    .commitAllowingStateLoss()
            }
        } else {
            homeLimitFmt?.onOkiloHomeLimitRefreshData()
        }
    }

    /**
     * 显示首页申贷状态
     */
    @SuppressLint("CommitTransaction")
    private fun sOkiloHomeApplyStatusPage(){
        homeLimitFmt = null
        if (homeApplyStatusFmt == null){
            homeApplyStatusFmt = OkiloHomeApplyStatusFmt()
            homeApplyStatusFmt?.let {
                childFragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
                    .replace(rootViewBinding.rootContainer.id,it)
                    .commitAllowingStateLoss()
            }
        } else {
            homeApplyStatusFmt?.onOkiloHomeApplyStatusRefreshData()
        }
    }

    override fun onOkiloPublishEvent(event: OkiloEventData) {
        super.onOkiloPublishEvent(event)
        when (event.eventType) {
            OkiloEventData.TYPE.OKILO_DATA_REFRESH,
            OkiloEventData.TYPE.OKILO_PROCESS_REFRESHED -> { //刷新流程完成
                checkOkiloHomeShowPage()
            }
            else -> {

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        homeLimitFmt = null
        homeApplyStatusFmt = null
    }
}